<?php 
  /**
   * Kontrollbase
   *
   * An open source MySQL monitoring and analytics application
   *
   * @package Kontrollbase
   * @author Matt Reid
   * @copyright Copyright (c) 2009 Matt Reid, Kontrollbase LLC
   * @license http://kontrollsoft.com/kontrollbase/userguide/general-LICENSE.php
   * @link http://kontrollbase.com
   */

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Model_hosts extends Model
{

  function __construct()
  {
    $this->Model_hosts();
  }
  
  function Model_hosts()
  {
    parent::Model();
  }

  function delete_data($id) {
    log_message('debug', "model_hosts: delete_data called");
    log_message('debug', "model_hosts: id: $id");
    $id = $this->db->escape_str($id);

    $dbr = $this->load->database('write', TRUE);
    $sql0="DELETE from `host_list` WHERE id = '$id' LIMIT 1;";
    log_message('debug', "$sql0");

    $this->db->trans_start();
    $this->db->query($sql0);
    $this->db->trans_complete();
    if ($this->db->trans_status() === FALSE) {
      log_message('debug', "model_hosts delete_data transaction failed");
      $this->db->trans_off();
      return 1;
    } 
    else {
      log_message('debug', "model_hosts delete_data transaction success");
      $this->db->trans_off();
      return 0;
    }
  }


  function update_data(
                       $id,
                       $field,
                       $value) {

    log_message('debug', "model_hosts: update_data called");
    log_message('debug', "model_hosts: id: $id, field: $field, value: $value");
    $id = $this->db->escape_str($id);
    $field = $this->db->escape_str($field);
    $value = $this->db->escape_str($value);
    
    $dbr = $this->load->database('write', TRUE);
    $sql0="UPDATE `host_list` SET $field = '$value' where id = '$id'";
    log_message('debug', "$sql0");

    $this->db->trans_start();
    $this->db->query($sql0);
    $this->db->trans_complete();
    if ($this->db->trans_status() === FALSE) {
      log_message('debug', "model_hosts updated_data transaction failed");
      $this->db->trans_off();
      return 1;
    } 
    else {
      log_message('debug', "model_hosts updated_data transaction success");
      $this->db->trans_off();
      return 0;
    }
  }

  function add_data($UUID) {    
    $dbr = $this->load->database('write', TRUE);
    $UUID = $this->db->escape_str($UUID);
    $sql0="INSERT INTO `host_list` (
`id` ,
`host_list_hostname` ,
`host_list_ipaddress` ,
`host_list_uuid` 
)
VALUES (
NULL, 
'hostname',
'0.0.0.0',
'$UUID');";

    $this->db->trans_start();
    $this->db->query($sql0);
    $this->db->trans_complete();
    if ($this->db->trans_status() === FALSE) {
      show_error('Could not add new row. Transaction failed.');
      $this->db->trans_off();
      return 1;
    } 
    else {
      $this->db->trans_off();
      return 0;
    }
  }

  function get_data() {
    log_message('debug', "model_hosts::get_data function starting.");
    $dbr = $this->load->database('read', TRUE);
    $sql = "select t1.*, t2.template_list_name from host_list as t1, template_list as t2 where t1.template_list_id = t2.id;";
    $query = $dbr->query($sql);
    if($query->num_rows() > 0) {
      log_message('debug', "get_data:: returning data");
      return $query->result_array();
    }
    else {
      log_message('debug', "get_data:: no data to display");
    }
  }

  function get_lastid() {
    log_message('debug', "model_hosts::get_lastid function starting.");
    $dbr = $this->load->database('read', TRUE);
    $sql = "select id from host_list order by id desc limit 1;";
    $query = $dbr->query($sql);
    if($query->num_rows() > 0) {
      log_message('debug', "get_lastid:: returning data");
      foreach ($query->result() as $row) {
        $data = $row->id;
	return $data;
      }
    }
    else {
      log_message('debug', "get_lastid:: no data to display");
    }
  }

}
?>